package org.dromara.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.DemoTree;
import org.dromara.demo.service.TestTreeServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 测试树表Controller
 *
 *
 * @date 2021-07-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/tree")
public class DemoTreeController extends BaseService {

  private final TestTreeServiceImpl treeService;

  /**
   * 查询测试树表列表
   */
  @SaCheckPermission("demo:tree:list")
  @GetMapping("/list")
  public R<List<DemoTree>> list(@Validated(QueryGroup.class) DemoTree bo) {
    List<DemoTree> list = treeService.queryList(bo);
    return R.ok(list);
  }

  /**
   * 导出测试树表列表
   */
  @SaCheckPermission("demo:tree:export")
  @Log(title = "测试树表", businessType = BusinessType.EXPORT)
  @GetMapping("/export")
  public void export(@Validated DemoTree bo, HttpServletResponse response) {
    List<DemoTree> list = treeService.queryList(bo);
    ExcelUtil.exportExcel(list, "测试树表", DemoTree.class, response);
  }

  /**
   * 获取测试树表详细信息
   *
   * @param id 测试树ID
   */
  @SaCheckPermission("demo:tree:query")
  @GetMapping("/{id}")
  public R<DemoTree> getInfo(@NotNull(message = "主键不能为空")
                             @PathVariable("id") Long id) {
    return R.ok(treeService.queryById(id));
  }

  /**
   * 新增测试树表
   */
  @SaCheckPermission("demo:tree:add")
  @Log(title = "测试树表", businessType = BusinessType.INSERT)
  @RepeatSubmit
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody DemoTree bo) {
    return toAjax(treeService.insertByBo(bo));
  }

  /**
   * 修改测试树表
   */
  @SaCheckPermission("demo:tree:edit")
  @Log(title = "测试树表", businessType = BusinessType.UPDATE)
  @RepeatSubmit
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody DemoTree bo) {
    return toAjax(treeService.updateByBo(bo));
  }

  /**
   * 删除测试树表
   *
   * @param ids 测试树ID串
   */
  @SaCheckPermission("demo:tree:remove")
  @Log(title = "测试树表", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空")
                        @PathVariable Long[] ids) {
    return toAjax(treeService.deleteWithValidByIds(Arrays.asList(ids), true));
  }
}
